<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

define( "JIEQI_MODULE_NAME", "system" );
require_once( "../global.php" );
jieqi_getconfigs( JIEQI_MODULE_NAME, "power" );
jieqi_checkpower( $jieqiPower['system']['manageallparlor'], $jieqiUsersStatus, $jieqiUsersGroup, false, true );
jieqi_getconfigs( JIEQI_MODULE_NAME, "configs" );
include_once( JIEQI_ROOT_PATH."/class/ptopics.php" );
$ptopics_handler =& jieqiptopicshandler::getinstance( "JieqiPtopicsHandler" );
$query = jieqiqueryhandler::getinstance( "JieqiQueryHandler" );
if ( isset( $_REQUEST['action'], $_REQUEST['tid'] ) )
{
		$actptopic = $ptopics_handler->get( $_REQUEST['tid'] );
		if ( is_object( $actptopic ) )
		{
				$criteria = new criteriacompo( new criteria( "topicid", $_REQUEST['tid'] ) );
				switch ( $_REQUEST['action'] )
				{
				case "top" :
						$ptopics_handler->updatefields( array( "istop" => 1 ), $criteria );
						break;
				case "untop" :
						$ptopics_handler->updatefields( array( "istop" => 0 ), $criteria );
						break;
				case "good" :
						$ptopics_handler->updatefields( array( "isgood" => 1 ), $criteria );
						break;
				case "normal" :
						$ptopics_handler->updatefields( array( "isgood" => 0 ), $criteria );
				case "del" :
						$ptopics_handler->delete( $criteria );
						$query->execute( "DELETE FROM ".jieqi_dbprefix( "system_pposts" )." WHERE topicid=".intval( $_REQUEST['tid'] ) );
				}
				unset( $criteria );
		}
}
else if ( isset( $_REQUEST['batchdel'] ) && $_REQUEST['batchdel'] == 1 && is_array( $_REQUEST['checkid'] ) && 0 < count( $_REQUEST['checkid'] ) )
{
		$where = "";
		foreach ( $GLOBALS['_REQUEST']['checkid'] as $v )
		{
				if ( is_numeric( $v ) )
				{
						$v = intval( $v );
						if ( !empty( $where ) )
						{
								$where .= ", ";
						}
						$where .= $v;
				}
		}
		if ( !empty( $where ) )
		{
				$sql = "DELETE FROM ".jieqi_dbprefix( "system_ptopics" )." WHERE topicid IN (".$where.")";
				$query->execute( $sql );
				$sql = "DELETE FROM ".jieqi_dbprefix( "system_pposts" )." WHERE topicid IN (".$where.")";
				$query->execute( $sql );
		}
}
include_once( JIEQI_ROOT_PATH."/admin/header.php" );
include_once( JIEQI_ROOT_PATH."/lib/text/textfunction.php" );
$jieqiTpl->assign( "checkall", "<input type=\"checkbox\" id=\"checkall\" name=\"checkall\" value=\"checkall\" onclick=\"javascript: for (var i=0;i<this.form.elements.length;i++){ if (this.form.elements[i].name != 'checkkall') this.form.elements[i].checked = form.checkall.checked; }\">" );
include_once( JIEQI_ROOT_PATH."/class/users.php" );
$criteria = new criteriacompo( );
$criteria->setfields( "t.*,u.uname,u.name" );
$criteria->settables( jieqi_dbprefix( "system_ptopics" )." AS t LEFT JOIN ".jieqi_dbprefix( "system_users" )." AS u ON t.ownerid=u.uid" );
if ( !empty( $_REQUEST['keyword'] ) )
{
		$GLOBALS['_REQUEST']['keyword'] = trim( $_REQUEST['keyword'] );
		if ( $_REQUEST['keytype'] == 1 )
		{
				$criteria->add( new criteria( "t.poster", $_REQUEST['keyword'], "=" ) );
		}
		else if ( $_REQUEST['keytype'] == 2 )
		{
				$criteria->add( new criteria( "t.title", "%".$_REQUEST['keyword']."%", "like" ) );
		}
		else
		{
				$criteria->add( new criteria( "u.uname", $_REQUEST['keyword'], "=" ) );
		}
}
if ( isset( $_REQUEST['type'] ) && $_REQUEST['type'] == "good" )
{
		$criteria->add( new criteria( "isgood", 1 ) );
}
else
{
		$GLOBALS['_REQUEST']['type'] = "all";
}
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) )
{
		$GLOBALS['_REQUEST']['page'] = 1;
}
$criteria->setsort( "topicid" );
$criteria->setorder( "DESC" );
$criteria->setlimit( $jieqiConfigs['system']['ptopicpnum'] );
$criteria->setstart( ( $_REQUEST['page'] - 1 ) * $jieqiConfigs['system']['ptopicpnum'] );
$query->queryobjects( $criteria );
$ptopicrows = array( );
$k = 0;
while ( $v = $query->getobject( ) )
{
		$start = 3;
		if ( $v->getvar( "istop" ) == 1 )
		{
				$ptopicrows[$k]['istop'] = 1;
				$start += 4;
		}
		else
		{
				$ptopicrows[$k]['istop'] = 0;
		}
		if ( $v->getvar( "isgood" ) == 1 )
		{
				$ptopicrows[$k]['isgood'] = 1;
				$start += 4;
		}
		else
		{
				$ptopicrows[$k]['isgood'] = 0;
		}
		$ptopicrows[$k]['topicid'] = $v->getvar( "topicid" );
		$ptopicrows[$k]['posttime'] = $v->getvar( "posttime" );
		$ptopicrows[$k]['posterid'] = $v->getvar( "posterid" );
		$ptopicrows[$k]['poster'] = $v->getvar( "poster" );
		$ptopicrows[$k]['title'] = $v->getvar( "title" );
		$ptopicrows[$k]['views'] = $v->getvar( "views" );
		$ptopicrows[$k]['replies'] = $v->getvar( "replies" );
		$ptopicrows[$k]['url_top'] = jieqi_addurlvars( array(
				"action" => "top",
				"rid" => $v->getvar( "topicid" )
		) );
		$ptopicrows[$k]['url_untop'] = jieqi_addurlvars( array(
				"action" => "untop",
				"rid" => $v->getvar( "topicid" )
		) );
		$ptopicrows[$k]['url_good'] = jieqi_addurlvars( array(
				"action" => "good",
				"rid" => $v->getvar( "topicid" )
		) );
		$ptopicrows[$k]['url_normal'] = jieqi_addurlvars( array(
				"action" => "normal",
				"rid" => $v->getvar( "topicid" )
		) );
		$ptopicrows[$k]['url_delete'] = jieqi_addurlvars( array(
				"action" => "del",
				"rid" => $v->getvar( "topicid" )
		) );
		$ptopicrows[$k]['checkbox'] = "<input type=\"checkbox\" id=\"checkid[]\" name=\"checkid[]\" value=\"".$v->getvar( "topicid" )."\">";
		$ptopicrows[$k]['articleid'] = $v->getvar( "articleid" );
		$ptopicrows[$k]['articlename'] = $v->getvar( "articlename" );
		$ptopicrows[$k]['ownerid'] = $v->getvar( "ownerid" );
		$ptopicrows[$k]['ownername'] = strlen( $v->getvar( "name" ) ) == 0 ? $v->getvar( "uname" ) : $v->getvar( "name" );
		++$k;
}
$jieqiTpl->assign_by_ref( "ptopicrows", $ptopicrows );
include_once( JIEQI_ROOT_PATH."/lib/html/page.php" );
$jumppage = new jieqipage( $query->getcount( $criteria ), $jieqiConfigs['system']['ptopicpnum'], $_REQUEST['page'] );
$jumppage->setlink( "", true, true );
$jieqiTpl->assign( "url_jumppage", $jumppage->whole_bar( ) );
$jieqiTpl->setcaching( 0 );
$jieqiTset['jieqi_contents_template'] = JIEQI_ROOT_PATH."/templates/admin/ptopiclist.html";
include_once( JIEQI_ROOT_PATH."/admin/footer.php" );
?>
